﻿@namespace AntDesign
@inherits AntDomComponentBase

<li
    title="@(ShowTitle ? Page : null)"
    class="@ClassMapper.Class"
    @onclick="HandleClick"
    @onkeypress="HandleKeyPress"
    tabIndex="0"
    @attributes="@UnmatchedAttributes"
    >
    @ItemRender(new PaginationItemRenderContext(Page, PaginationItemType.Page, _ => @<a rel="nofollow">@Page</a>, Disabled))
</li>

@code {

    [Parameter]
    public bool ShowTitle { get; set; }

    [Parameter]
    public int Page { get; set; }

    [Parameter]
    public string RootPrefixCls { get; set; }

    [Parameter]
    public bool Active { get; set; }
    
    [Parameter]
    public bool Disabled { get; set; }

    [Parameter]
    public EventCallback<int> OnClick { get; set; }

    [Parameter]
    public EventCallback<(KeyboardEventArgs e, Action callback)> OnKeyPress { get; set; }

    [Parameter]
    public RenderFragment<PaginationItemRenderContext> ItemRender { get; set; }

    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object> UnmatchedAttributes { get; set; }

    protected override void OnInitialized()
    {
        var prefixCls = $"{RootPrefixCls}-item";
        ClassMapper.Add(prefixCls).Add($"{prefixCls}-{Page}")
                   .If($"{prefixCls}-active", () => Active)
                   .If(Class, () => !string.IsNullOrWhiteSpace(Class))
                   .If($"{prefixCls}-disabled", () => Page == 0);

        base.OnInitialized();
    }

    private async Task HandleClick()
    {
        if (OnClick.HasDelegate)
        {
            await OnClick.InvokeAsync(Page);
        }
    }

    private async Task HandleKeyPress(KeyboardEventArgs e)
    {
        if (OnKeyPress.HasDelegate)
        {
            await OnKeyPress.InvokeAsync((e, async () =>
            {
                if (OnClick.HasDelegate)
                {
                    await OnClick.InvokeAsync(Page);
                }
            }));
        }
    }

}